using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class UArrowControl : Control
    {
        private double value = 0;
        private double minValue = 0;
        private double maxValue = 100;
        private double startAngle = 135;
        private double sweepAngle = 270;
        private Color arrowColor = Color.Red;

        [Category("数据")]
        [Description("当前值")]
        public double Value
        {
            get { return value; }
            set 
            {
                this.value = Math.Max(minValue, Math.Min(maxValue, value));
                this.Invalidate();
            }
        }

        [Category("数据")]
        [Description("最小值")]
        public double MinValue
        {
            get { return minValue; }
            set 
            {
                minValue = value;
                this.Value = Math.Max(minValue, this.value);
                this.Invalidate();
            }
        }

        [Category("数据")]
        [Description("最大值")]
        public double MaxValue
        {
            get { return maxValue; }
            set 
            {
                maxValue = value;
                this.Value = Math.Min(maxValue, this.value);
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("起始角度")]
        public double StartAngle
        {
            get { return startAngle; }
            set 
            {
                startAngle = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("扫描角度")]
        public double SweepAngle
        {
            get { return sweepAngle; }
            set 
            {
                sweepAngle = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("箭头颜色")]
        public Color ArrowColor
        {
            get { return arrowColor; }
            set 
            {
                arrowColor = value;
                this.Invalidate();
            }
        }

        public UArrowControl()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            int centerX = this.Width / 2;
            int centerY = this.Height / 2;
            int radius = Math.Min(centerX, centerY) - 5;

            // 计算箭头角度
            double ratio = (value - minValue) / (maxValue - minValue);
            double angle = startAngle + sweepAngle * ratio;
            double radians = angle * Math.PI / 180;

            // 计算箭头终点
            int endX = centerX + (int)(Math.Cos(radians) * radius * 0.8);
            int endY = centerY - (int)(Math.Sin(radians) * radius * 0.8);

            // 绘制箭头
            using (Pen pen = new Pen(arrowColor, 2))
            {
                g.DrawLine(pen, centerX, centerY, endX, endY);
            }

            // 绘制箭头头部
            using (Brush brush = new SolidBrush(arrowColor))
            {
                PointF[] arrowHead = new PointF[3];
                arrowHead[0] = new PointF(endX, endY);
                arrowHead[1] = new PointF(
                    endX - 8 * (float)Math.Cos(radians - Math.PI / 6),
                    endY + 8 * (float)Math.Sin(radians - Math.PI / 6)
                );
                arrowHead[2] = new PointF(
                    endX - 8 * (float)Math.Cos(radians + Math.PI / 6),
                    endY + 8 * (float)Math.Sin(radians + Math.PI / 6)
                );
                g.FillPolygon(brush, arrowHead);
            }

            // 绘制中心点
            using (Brush brush = new SolidBrush(Color.Black))
            {
                g.FillEllipse(brush, centerX - 3, centerY - 3, 6, 6);
            }
        }
    }
}